#ifndef GAME_ANIMATION_GRID_GRID_H
#define GAME_ANIMATION_GRID_GRID_H
//
// Game Animation.
// Copyright (C) 2007 Department of Computer Science, University of Copenhagen
//
#include <animation/configuration.h>

#include <vector>
#include <cassert>

namespace animation
{
  /**
  * A Virtual 2D grid of Grid Cells.
  * This class is basically just a container class, with functionality
  * to iterate over all cells and accessing a specific cell given its
  * local (s,t) indices.
  */
  template<typename types>
  class Grid : public types::grid_locator
  {
  public:

    typedef typename types::grid_locator             base_class;
    typedef typename types::index_type               index_type;
    typedef typename types::real_type                real_type;
    typedef typename types::cell_type                cell_type;
    typedef std::vector<cell_type>                   cell_array;
    typedef typename cell_array::iterator            cell_iterator;
    typedef typename cell_array::const_iterator      const_cell_iterator;

  protected:

    cell_array    m_cells;  ///< The cells stored in this grid.

  public:

    cell_iterator        begin(){return m_cells.begin(); }
    cell_iterator        end()  {return m_cells.end();   }
    const_cell_iterator  begin() const {return m_cells.begin(); }
    const_cell_iterator  end()   const {return m_cells.end();   }

  public:

    index_type const size()const { return m_cells.size(); }

  public:

    cell_type & cell(index_type const & s, index_type const & t){ return m_cells[t*(this->m_I) + s]; }
    cell_type const & cell(index_type const & s, index_type const & t) const {  return m_cells[t*(this->m_I) + s]; }

  public:


    Grid()
    {
      this->m_I = 0;
      this->m_J = 0;
      m_cells.resize(0);
    }

    /**
     * Initialization.
     *
     * @param I                 Number of grid cells along x-axis.
     * @param J                 Number of grid cells along y-axis.
     * @param dx                The length of the grid cells along the x-axis.
     * @param dy                The length of the grid cells along the y-axis.
     */
    void init(index_type const & I,index_type const & J,real_type const & dx,real_type const & dy)
    {
      base_class::init(I,J,dx,dy);
      m_cells.resize(I*J);
    }

  };

} // namespace animation

// GAME_ANIMATION_GRID_GRID_H
#endif
